<?php

// No direct access
defined ('_JEXEC') or die();

jimport ('joomla.application.component.modelform');

class ShipnhanhModelOrder extends JModelForm
{
	public function getForm($data = array(), $loadData = true)
	{
		$app = JFactory::getApplication('site');
		// Get form
		$form = $this->loadForm('com_shipnhanh.order', 'order', array('control' => 'jform', 'load_data' => true));
		
		if (! $form)
		{
			return false;
		}
		else
		{
			return $form;
		}
	}
	
	public function insertOrder($data)
	{
		$db = JFactory::getDBO();
		$query = $db->getQuery(true);
		
		$productName = $db->quote($data['productname']);
		$link = $db->quote($data['link']);
		$photo_link = $db->quote($data['img_url']);
		$color = $db->quote($data['color']);
		$size = $db->quote($data['size']);
		$quantity = $data['quantity'];
		$webprice = $db->quote($data['webprice']);
		$phone = $db->quote($data['phone']);
		$note = $db->quote($data['note']);
		$userid = JFactory::getUser()->id;
		
		$columns = array('product_name', 'link', 'photo_link', 'color', 'size', 'quantity', 'web_price', 'phone', 'note', 'read', 'userid', 'date');
		$values = array($productName, $link, $photo_link, $color, $size, $quantity, $webprice, $phone, $note, 0, $userid, 'now()');
		
		$query->insert('#__sn_user_orders');
		$query->columns($db->quoteName($columns));
		$query->values(implode(',', $values));
		
		$db->setQuery($query);
		
		if (!$db->query())
		{
			JError::raiseError(500, $db->getErrorMsg());
			return false;
		}
		else
		{
			return true;
		}
	}
}